﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using Barbar.MapReport.Common.Drawing.ValueIndicators;
using Barbar.MapReport.HtmlRender.Shapes;

namespace Barbar.MapReport.HtmlRender.Operations.ValueIndicators {
  internal class ValueIndicatorAgregatorListRenderer : IValueIndicatorAgregatorRenderer {
    private ListProperties m_Properties;

    public ValueIndicatorAgregatorListRenderer(ListProperties properties) {
      m_Properties = properties;
    }

    internal static string CreateToolTip(string toolTipTemplate, IList<FlatPoint> items, int maxToolTipAgregated) {
      StringBuilder builder = new StringBuilder();
      int toolTipsCount = 0;
      foreach (FlatPoint valueIndicator in items) {
        builder.Append(valueIndicator.ToolTipForAggregation);
        toolTipsCount++;
        if (toolTipsCount >= maxToolTipAgregated)
          break;
      }

      string toolTip = null;
      if (toolTipTemplate != null && items.Count > 1) {
        toolTip = toolTipTemplate.Replace("{Count}", Convert.ToString(items.Count, CultureInfo.InvariantCulture)).Replace("{Content}", builder.ToString());
      } else
        toolTip = builder.ToString();
      return toolTip;
    }

    #region IValueIndicatorAgregatorRenderer Members

    public ShapeWithTooltip Render(ValueIndicatorAgregator agregator) {
      int realRadius = agregator.RealRadius;
      IShape shape;
      if (agregator.Items.Count <= 1)
        shape = new CircleShape(realRadius, m_Properties.Color.Color);
      else
        shape = new CircleWithNumberShape(realRadius, m_Properties.Color.Color, agregator.Items.Count);

      string toolTip = CreateToolTip(m_Properties.ToolTipTemplate, agregator.Items, m_Properties.MaxToolTipAggregated);
      return new ShapeWithTooltip(shape, toolTip);
    }
    

    #endregion
  }
}
